﻿using System.Collections.ObjectModel;
using System.Windows.Input;
using MainGameModule.Models;
using MainGameModule.Views;
using Microsoft.Practices.Composite.Presentation.Commands;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
using YourWorld.Common;
using BusinessObjects;
using System;

namespace MainGameModule.ViewModels
{
    public class GameEventsViewModel : ViewModelBase
    {
        private IRegionManager regionManager;
        private IUnityContainer container;
        private IGameModel gameModel;

        private ObservableCollection<Event> eventsList;
        private Event selectedEvent;

        private string greeting;
        private string startGreeting;

        public event Action<Event> StartQuest;

        public GameEventsViewModel()
        {
        }

        public GameEventsViewModel( IGameModel gameModel, IRegionManager regionManager, IUnityContainer container )
        {
            this.gameModel = gameModel;
            this.regionManager = regionManager;
            this.container = container;

            this.CloseEventsCommand = new DelegateCommand<object>( this.CloseEventsView );
            this.StartEventCommand = new DelegateCommand<object>( this.StartEvent );

            this.EventsList = RegionObjectsRepository.Instance.EventsList;
            this.Initialize();
        }

        public ObservableCollection<Event> EventsList
        {
            get
            {
                if ( this.eventsList == null )
                    this.eventsList = new ObservableCollection<Event>();

                return this.eventsList;
            }
            set
            {
                if ( this.eventsList == value )
                    return;

                this.eventsList = value;
                this.NotifyPropertyChanged( "EventsList" );
            }
        }

        public Event SelectedEvent
        {
            get
            {
                return this.selectedEvent;
            }
            set
            {
                if ( this.selectedEvent == value )
                    return;

                this.selectedEvent = value;
                this.NotifyPropertyChanged( "SelectedEvent" );

                this.Initialize();
            }
        }

        public string Greeting
        {
            get
            {
                return this.greeting;
            }
            set
            {
                if ( this.greeting == value )
                    return;

                this.greeting = value;
                this.NotifyPropertyChanged( "Greeting" );
            }
        }

        public string StartGreeting
        {
            get
            {
                return this.startGreeting;
            }
            set
            {
                if ( this.startGreeting == value )
                    return;

                this.startGreeting = value;
                this.NotifyPropertyChanged( "StartGreeting" );
            }
        }

        public ICommand StartEventCommand
        {
            get;
            private set;
        }

        public ICommand CloseEventsCommand
        {
            get;
            private set;
        }

        public void Initialize()
        {
            if ( this.SelectedEvent == null )
            {
                this.Greeting = "These are the quests. This is your chance to step up and make a difference! Be the first to take all the quests! You can connect with your frinds and take bigger quests together! Good Luck!";
                return;
            }

            if ( this.SelectedEvent.Type == RegionObjectTypes.Causes )
            {
                this.Greeting = "Be the first to join this quest and spread the news amongst your friends!";
                this.StartGreeting = " Join ";
            }
            else if ( this.SelectedEvent.Type == RegionObjectTypes.Build )
            {
                this.Greeting = "Be the first to make a change!";
                this.StartGreeting = " Build ";
            }
        }

        private void CloseEventsView( object o )
        {
            this.regionManager.Regions[RegionNames.GamePopUpRegion].Deactivate( this.container.Resolve<GameEventsView>() );
        }

        private void StartEvent( object o )
        {
            if ( this.SelectedEvent != null )
            {
                GameMainViewModel gmvm = this.container.Resolve<GameMainViewModel>();
                gmvm.BuildQuestRequest( this.SelectedEvent );
            }

            this.CloseEventsView( null );
        }
    }
}
